package com.sgcc.pda.frme.protocol.ws.sendbean;

import androidx.annotation.NonNull;

import android.text.TextUtils;

import com.sgcc.pda.frme.protocol.ws.DataConvert;

/**
 * @author guorongxin
 * @date 2020/3/25
 * @fileName hw
 * @Description
 * @special 如修改此类请在下方添加备注并告知创建者，谢谢配合！
 * @VERSION 1.0
 */
public class EpcChoiceLabel2006 {
    /**
     * 类型
     */
    private String type;

    /**
     * 偏移
     */
    private String offset;

    /**
     * 长度
     */
    private String length;
    /**
     * 匹配数据
     */
    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getOffset() {
        return offset;
    }

    public void setOffset(String offset) {
        this.offset = offset;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (null == type) {
            return null;
        }
        if (type.length() != 2) {
            type = lengthfilling(type, 2);
        }
        sb.append(type);

        if (null == offset) {
            return null;
        }
        if (offset.length() != 4) {
            offset = lengthfilling(offset, 4);
        }
        sb.append(DataConvert.strReverse(offset, 0, offset.length()));
        if (null == length) {
            return null;
        }
        if (length.length() != 2) {
            length = lengthfilling(length, 2);
        }
        sb.append(length);

        if (null == data) {
            return null;
        }
        sb.append(DataConvert.strReverse(data, 0, data.length()));
        return sb.toString();
    }

    public String lengthfilling(String meterAddress, int length) {
        if (!TextUtils.isEmpty(meterAddress) && meterAddress.length() < length) {
            StringBuilder stringBuffer = new StringBuilder();
            for (int i = 0; i < length - meterAddress.length(); i++) {
                stringBuffer.append("0");
            }
            stringBuffer.append(meterAddress);
            meterAddress = stringBuffer.toString();
            return meterAddress;
        }
        return "";
    }
}
